#include "dot_custom_tiling.h"
#include "kernel_operator.h"

constexpr int32_t BUFFER_NUM = 2; // tensor num for each queue

constexpr uint32_t CEIL_BFLOAT16 = 0;
constexpr uint32_t CEIL_FLOAT16 = 1;
constexpr uint32_t CEIL_FLOAT32 = 2;
constexpr uint32_t CEIL_INT8 = 3;
constexpr uint32_t CEIL_INT16 = 4;
constexpr uint32_t CEIL_INT32 = 5;

constexpr uint32_t LAST_TWO_TILE = 2;

template <typename dataType>
class KernelDot;

template <>
class KernelDot<bfloat16_t> {
public:
    __aicore__ inline KernelDot() {}
    __aicore__ inline void Init(GM_ADDR x, GM_ADDR y, GM_ADDR z, DotCustomTilingData tiling)
    {   
        if (tiling.isEvenCore)
        {
            this->blockLength = tiling.blockLength;
            this->tileNum = tiling.tileNum;
            this->tileLength = tiling.tileLength / BUFFER_NUM;
            this->lastTileLength = tiling.lastTileLength;

            xGm.SetGlobalBuffer((__gm__ bfloat16_t *)x + this->blockLength * AscendC::GetBlockIdx(), this->blockLength);
            yGm.SetGlobalBuffer((__gm__ bfloat16_t *)y + this->blockLength * AscendC::GetBlockIdx(), this->blockLength);
            zGm.SetGlobalBuffer((__gm__ bfloat16_t *)z + this->blockLength * AscendC::GetBlockIdx(), this->blockLength);
        }
        else
        {
            if (AscendC::GetBlockIdx() < tiling.formerNum)
            {
                this->tileNum = tiling.formerTileNum;
                this->tileLength = tiling.formerTileLength / BUFFER_NUM;
                this->lastTileLength = tiling.formerLastTileLength;

                xGm.SetGlobalBuffer((__gm__ bfloat16_t *)x + tiling.formerLength * AscendC::GetBlockIdx(),
                                    tiling.formerLength);
                yGm.SetGlobalBuffer((__gm__ bfloat16_t *)y + tiling.formerLength * AscendC::GetBlockIdx(),
                                    tiling.formerLength);
                zGm.SetGlobalBuffer((__gm__ bfloat16_t *)z + tiling.formerLength * AscendC::GetBlockIdx(),
                                    tiling.formerLength);
            }
            else
            {
                this->tileNum = tiling.tailTileNum;
                this->tileLength = tiling.tailTileLength / BUFFER_NUM;
                this->lastTileLength = tiling.tailLastTileLength;

                xGm.SetGlobalBuffer((__gm__ bfloat16_t *)x + tiling.formerLength * tiling.formerNum +
                                        tiling.tailLength * (AscendC::GetBlockIdx() - tiling.formerNum),
                                    tiling.tailLength);
                yGm.SetGlobalBuffer((__gm__ bfloat16_t *)y + tiling.formerLength * tiling.formerNum +
                                        tiling.tailLength * (AscendC::GetBlockIdx() - tiling.formerNum),
                                    tiling.tailLength);
                zGm.SetGlobalBuffer((__gm__ bfloat16_t *)z + tiling.formerLength * tiling.formerNum +
                                        tiling.tailLength * (AscendC::GetBlockIdx() - tiling.formerNum),
                                    tiling.tailLength);
            }
        }
        pipe.InitBuffer(inQueueX, BUFFER_NUM, this->tileLength * sizeof(bfloat16_t));
        pipe.InitBuffer(inQueueY, BUFFER_NUM, this->tileLength * sizeof(bfloat16_t));
        pipe.InitBuffer(outQueueZ, BUFFER_NUM, this->tileLength * sizeof(bfloat16_t));
        pipe.InitBuffer(tmpBuf0, this->tileLength * sizeof(float));
        pipe.InitBuffer(tmpBuf1, this->tileLength * sizeof(float));
        pipe.InitBuffer(tmpBuf2, this->tileLength * sizeof(float));
        pipe.InitBuffer(tmpBuf3, this->tileLength * sizeof(float));
    }
    __aicore__ inline void Process()
    {
        int32_t loopCount = this->tileNum * BUFFER_NUM;
        AscendC::LocalTensor<float> tmpTensor0 = tmpBuf0.Get<float>();
        AscendC::Duplicate(tmpTensor0, float(0.0f), this->tileLength);
        for (int32_t i = 0; i < loopCount; i++) {
            CopyIn(i);
            Compute(i, tmpTensor0);
            CopyOut(i);
        }
    }

private:
    __aicore__ inline void CopyIn(int32_t progress)
    {
        AscendC::LocalTensor<bfloat16_t> xLocal = inQueueX.AllocTensor<bfloat16_t>();
        AscendC::LocalTensor<bfloat16_t> yLocal = inQueueY.AllocTensor<bfloat16_t>();
        if ((progress == (this->tileNum * BUFFER_NUM - 2)) || (progress == (this->tileNum * BUFFER_NUM - 1)))
        {
            AscendC::DataCopy(xLocal, xGm[(progress - LAST_TWO_TILE) * this->tileLength + this->lastTileLength],
                              this->tileLength);
            AscendC::DataCopy(yLocal, yGm[(progress - LAST_TWO_TILE) * this->tileLength + this->lastTileLength],
                              this->tileLength);
        }
        else
        {
            AscendC::DataCopy(xLocal, xGm[progress * this->tileLength], this->tileLength);
            AscendC::DataCopy(yLocal, yGm[progress * this->tileLength], this->tileLength);
        }
        inQueueX.EnQue(xLocal);
        inQueueY.EnQue(yLocal);
    }
    __aicore__ inline void Compute(int32_t progress, AscendC::LocalTensor<float> tmpTensor0)
    {
        AscendC::LocalTensor<bfloat16_t> xLocal = inQueueX.DeQue<bfloat16_t>();
        AscendC::LocalTensor<bfloat16_t> yLocal = inQueueY.DeQue<bfloat16_t>();
        AscendC::LocalTensor<bfloat16_t> zLocal = outQueueZ.AllocTensor<bfloat16_t>();
        AscendC::LocalTensor<float> tmpTensor1 = tmpBuf1.Get<float>();
        AscendC::LocalTensor<float> tmpTensor2 = tmpBuf2.Get<float>();
        AscendC::LocalTensor<float> tmpTensor3 = tmpBuf3.Get<float>();

        AscendC::Cast(tmpTensor1, xLocal, AscendC::RoundMode::CAST_NONE, this->tileLength);
        AscendC::Cast(tmpTensor2, yLocal, AscendC::RoundMode::CAST_NONE, this->tileLength);
        AscendC::Mul(tmpTensor1, tmpTensor1, tmpTensor2, this->tileLength);
        AscendC::Duplicate(tmpTensor3, float(0.0f), this->tileLength);
        AscendC::ReduceSum(tmpTensor3, tmpTensor1, tmpTensor2, this->tileLength);
        AscendC::Add(tmpTensor3, tmpTensor0, tmpTensor3, this->tileLength);
        AscendC::DataCopy(tmpTensor0, tmpTensor3, this->tileLength);
        AscendC::Cast(zLocal, tmpTensor3, AscendC::RoundMode::CAST_RINT, this->tileLength);

        outQueueZ.EnQue<bfloat16_t>(zLocal);
        inQueueX.FreeTensor(xLocal);
        inQueueY.FreeTensor(yLocal);      
    }
    __aicore__ inline void CopyOut(int32_t progress)
    {
        AscendC::LocalTensor<bfloat16_t> zLocal = outQueueZ.DeQue<bfloat16_t>();
        AscendC::DataCopy(zGm[0], zLocal, this->tileLength);
        outQueueZ.FreeTensor(zLocal);
    }

private:
    AscendC::TPipe pipe;
    AscendC::TQue<AscendC::TPosition::VECIN, BUFFER_NUM> inQueueX;
    AscendC::TQue<AscendC::TPosition::VECIN, BUFFER_NUM> inQueueY;
    AscendC::TQue<AscendC::TPosition::VECOUT, BUFFER_NUM> outQueueZ;
    AscendC::TBuf<AscendC::TPosition::VECCALC> tmpBuf0;
    AscendC::TBuf<AscendC::TPosition::VECCALC> tmpBuf1;
    AscendC::TBuf<AscendC::TPosition::VECCALC> tmpBuf2;
    AscendC::TBuf<AscendC::TPosition::VECCALC> tmpBuf3;
    AscendC::GlobalTensor<bfloat16_t> xGm;
    AscendC::GlobalTensor<bfloat16_t> yGm;
    AscendC::GlobalTensor<bfloat16_t> zGm;
    uint32_t blockLength;
    uint32_t tileNum;
    uint32_t tileLength;
    uint32_t lastTileLength;
};


template <>
class KernelDot<int8_t> {
public:
    __aicore__ inline KernelDot() {}
    __aicore__ inline void Init(GM_ADDR x, GM_ADDR y, GM_ADDR z, DotCustomTilingData tiling)
    {   
        if (tiling.isEvenCore)
        {
            this->blockLength = tiling.blockLength;
            this->tileNum = tiling.tileNum;
            this->tileLength = tiling.tileLength / BUFFER_NUM;
            this->lastTileLength = tiling.lastTileLength;

            xGm.SetGlobalBuffer((__gm__ int8_t *)x + this->blockLength * AscendC::GetBlockIdx(), this->blockLength);
            yGm.SetGlobalBuffer((__gm__ int8_t *)y + this->blockLength * AscendC::GetBlockIdx(), this->blockLength);
            zGm.SetGlobalBuffer((__gm__ int8_t *)z + this->blockLength * AscendC::GetBlockIdx(), this->blockLength);
        }
        else
        {
            if (AscendC::GetBlockIdx() < tiling.formerNum)
            {
                this->tileNum = tiling.formerTileNum;
                this->tileLength = tiling.formerTileLength / BUFFER_NUM;
                this->lastTileLength = tiling.formerLastTileLength;

                xGm.SetGlobalBuffer((__gm__ int8_t *)x + tiling.formerLength * AscendC::GetBlockIdx(),
                                    tiling.formerLength);
                yGm.SetGlobalBuffer((__gm__ int8_t *)y + tiling.formerLength * AscendC::GetBlockIdx(),
                                    tiling.formerLength);
                zGm.SetGlobalBuffer((__gm__ int8_t *)z + tiling.formerLength * AscendC::GetBlockIdx(),
                                    tiling.formerLength);
            }
            else
            {
                this->tileNum = tiling.tailTileNum;
                this->tileLength = tiling.tailTileLength / BUFFER_NUM;
                this->lastTileLength = tiling.tailLastTileLength;

                xGm.SetGlobalBuffer((__gm__ int8_t *)x + tiling.formerLength * tiling.formerNum +
                                        tiling.tailLength * (AscendC::GetBlockIdx() - tiling.formerNum),
                                    tiling.tailLength);
                yGm.SetGlobalBuffer((__gm__ int8_t *)y + tiling.formerLength * tiling.formerNum +
                                        tiling.tailLength * (AscendC::GetBlockIdx() - tiling.formerNum),
                                    tiling.tailLength);
                zGm.SetGlobalBuffer((__gm__ int8_t *)z + tiling.formerLength * tiling.formerNum +
                                        tiling.tailLength * (AscendC::GetBlockIdx() - tiling.formerNum),
                                    tiling.tailLength);
            }
        }
        pipe.InitBuffer(inQueueX, BUFFER_NUM, this->tileLength * sizeof(int8_t));
        pipe.InitBuffer(inQueueY, BUFFER_NUM, this->tileLength * sizeof(int8_t));
        pipe.InitBuffer(outQueueZ, BUFFER_NUM, this->tileLength * sizeof(int8_t));
        pipe.InitBuffer(tmpBuf0, this->tileLength * sizeof(half));
        pipe.InitBuffer(tmpBuf1, this->tileLength * sizeof(half));
        pipe.InitBuffer(tmpBuf2, this->tileLength * sizeof(half));
        pipe.InitBuffer(tmpBuf3, this->tileLength * sizeof(half));
    }
    __aicore__ inline void Process()
    {
        int32_t loopCount = this->tileNum * BUFFER_NUM;
        AscendC::LocalTensor<half> tmpTensor0 = tmpBuf0.Get<half>();
        AscendC::Duplicate(tmpTensor0, half(0.0f), this->tileLength);
        for (int32_t i = 0; i < loopCount; i++) {
            CopyIn(i);
            Compute(i, tmpTensor0);
            CopyOut(i);
        }
    }

private:
    __aicore__ inline void CopyIn(int32_t progress)
    {
        AscendC::LocalTensor<int8_t> xLocal = inQueueX.AllocTensor<int8_t>();
        AscendC::LocalTensor<int8_t> yLocal = inQueueY.AllocTensor<int8_t>();
        if ((progress == (this->tileNum * BUFFER_NUM - 2)) || (progress == (this->tileNum * BUFFER_NUM - 1)))
        {
            AscendC::DataCopy(xLocal, xGm[(progress - LAST_TWO_TILE) * this->tileLength + this->lastTileLength],
                              this->tileLength);
            AscendC::DataCopy(yLocal, yGm[(progress - LAST_TWO_TILE) * this->tileLength + this->lastTileLength],
                              this->tileLength);
        }
        else
        {
            AscendC::DataCopy(xLocal, xGm[progress * this->tileLength], this->tileLength);
            AscendC::DataCopy(yLocal, yGm[progress * this->tileLength], this->tileLength);
        }
        inQueueX.EnQue(xLocal);
        inQueueY.EnQue(yLocal);
    }
    __aicore__ inline void Compute(int32_t progress, AscendC::LocalTensor<half> tmpTensor0)
    {
        AscendC::LocalTensor<int8_t> xLocal = inQueueX.DeQue<int8_t>();
        AscendC::LocalTensor<int8_t> yLocal = inQueueY.DeQue<int8_t>();
        AscendC::LocalTensor<int8_t> zLocal = outQueueZ.AllocTensor<int8_t>();
        AscendC::LocalTensor<half> tmpTensor1 = tmpBuf1.Get<half>();
        AscendC::LocalTensor<half> tmpTensor2 = tmpBuf2.Get<half>();
        AscendC::LocalTensor<half> tmpTensor3 = tmpBuf3.Get<half>();

        AscendC::Cast(tmpTensor1, xLocal, AscendC::RoundMode::CAST_NONE, this->tileLength);
        AscendC::Cast(tmpTensor2, yLocal, AscendC::RoundMode::CAST_NONE, this->tileLength);
        AscendC::Mul(tmpTensor1, tmpTensor1, tmpTensor2, this->tileLength);
        AscendC::Duplicate(tmpTensor3, half(0.0f), this->tileLength);
        AscendC::ReduceSum(tmpTensor3, tmpTensor1, tmpTensor2, this->tileLength);
        AscendC::Add(tmpTensor3, tmpTensor0, tmpTensor3, this->tileLength);
        AscendC::DataCopy(tmpTensor0, tmpTensor3, this->tileLength);
        AscendC::Cast(zLocal, tmpTensor3, AscendC::RoundMode::CAST_RINT, this->tileLength);

        outQueueZ.EnQue<int8_t>(zLocal);
        inQueueX.FreeTensor(xLocal);
        inQueueY.FreeTensor(yLocal);       
    }
    __aicore__ inline void CopyOut(int32_t progress)
    {
        AscendC::LocalTensor<int8_t> zLocal = outQueueZ.DeQue<int8_t>();
        AscendC::DataCopy(zGm[0], zLocal, this->tileLength);
        outQueueZ.FreeTensor(zLocal);
    }

private:
    AscendC::TPipe pipe;
    AscendC::TQue<AscendC::TPosition::VECIN, BUFFER_NUM> inQueueX;
    AscendC::TQue<AscendC::TPosition::VECIN, BUFFER_NUM> inQueueY;
    AscendC::TQue<AscendC::TPosition::VECOUT, BUFFER_NUM> outQueueZ;
    AscendC::TBuf<AscendC::TPosition::VECCALC> tmpBuf0;
    AscendC::TBuf<AscendC::TPosition::VECCALC> tmpBuf1;
    AscendC::TBuf<AscendC::TPosition::VECCALC> tmpBuf2;
    AscendC::TBuf<AscendC::TPosition::VECCALC> tmpBuf3;
    AscendC::GlobalTensor<int8_t> xGm;
    AscendC::GlobalTensor<int8_t> yGm;
    AscendC::GlobalTensor<int8_t> zGm;
    uint32_t blockLength;
    uint32_t tileNum;
    uint32_t tileLength;
    uint32_t lastTileLength;
};


template <>
class KernelDot<int16_t> {
public:
    __aicore__ inline KernelDot() {}
    __aicore__ inline void Init(GM_ADDR x, GM_ADDR y, GM_ADDR z, DotCustomTilingData tiling)
    {   
        if (tiling.isEvenCore)
        {
            this->blockLength = tiling.blockLength;
            this->tileNum = tiling.tileNum;
            this->tileLength = tiling.tileLength / BUFFER_NUM;
            this->lastTileLength = tiling.lastTileLength;

            xGm.SetGlobalBuffer((__gm__ int16_t *)x + this->blockLength * AscendC::GetBlockIdx(), this->blockLength);
            yGm.SetGlobalBuffer((__gm__ int16_t *)y + this->blockLength * AscendC::GetBlockIdx(), this->blockLength);
            zGm.SetGlobalBuffer((__gm__ int16_t *)z + this->blockLength * AscendC::GetBlockIdx(), this->blockLength);
        }
        else
        {
            if (AscendC::GetBlockIdx() < tiling.formerNum)
            {
                this->tileNum = tiling.formerTileNum;
                this->tileLength = tiling.formerTileLength / BUFFER_NUM;
                this->lastTileLength = tiling.formerLastTileLength;

                xGm.SetGlobalBuffer((__gm__ int16_t *)x + tiling.formerLength * AscendC::GetBlockIdx(),
                                    tiling.formerLength);
                yGm.SetGlobalBuffer((__gm__ int16_t *)y + tiling.formerLength * AscendC::GetBlockIdx(),
                                    tiling.formerLength);
                zGm.SetGlobalBuffer((__gm__ int16_t *)z + tiling.formerLength * AscendC::GetBlockIdx(),
                                    tiling.formerLength);
            }
            else
            {
                this->tileNum = tiling.tailTileNum;
                this->tileLength = tiling.tailTileLength / BUFFER_NUM;
                this->lastTileLength = tiling.tailLastTileLength;

                xGm.SetGlobalBuffer((__gm__ int16_t *)x + tiling.formerLength * tiling.formerNum +
                                        tiling.tailLength * (AscendC::GetBlockIdx() - tiling.formerNum),
                                    tiling.tailLength);
                yGm.SetGlobalBuffer((__gm__ int16_t *)y + tiling.formerLength * tiling.formerNum +
                                        tiling.tailLength * (AscendC::GetBlockIdx() - tiling.formerNum),
                                    tiling.tailLength);
                zGm.SetGlobalBuffer((__gm__ int16_t *)z + tiling.formerLength * tiling.formerNum +
                                        tiling.tailLength * (AscendC::GetBlockIdx() - tiling.formerNum),
                                    tiling.tailLength);
            }
        }
        pipe.InitBuffer(inQueueX, BUFFER_NUM, this->tileLength * sizeof(int16_t));
        pipe.InitBuffer(inQueueY, BUFFER_NUM, this->tileLength * sizeof(int16_t));
        pipe.InitBuffer(outQueueZ, BUFFER_NUM, this->tileLength * sizeof(int16_t));
        pipe.InitBuffer(tmpBuf0, this->tileLength * sizeof(float));
        pipe.InitBuffer(tmpBuf1, this->tileLength * sizeof(float));
        pipe.InitBuffer(tmpBuf2, this->tileLength * sizeof(float));
        pipe.InitBuffer(tmpBuf3, this->tileLength * sizeof(float));
    }
    __aicore__ inline void Process()
    {
        int32_t loopCount = this->tileNum * BUFFER_NUM;
        AscendC::LocalTensor<float> tmpTensor0 = tmpBuf0.Get<float>();
        AscendC::Duplicate(tmpTensor0, float(0.0f), this->tileLength);
        for (int32_t i = 0; i < loopCount; i++) {
            CopyIn(i);
            Compute(i, tmpTensor0);
            CopyOut(i);
        }
    }

private:
    __aicore__ inline void CopyIn(int32_t progress)
    {
        AscendC::LocalTensor<int16_t> xLocal = inQueueX.AllocTensor<int16_t>();
        AscendC::LocalTensor<int16_t> yLocal = inQueueY.AllocTensor<int16_t>();
        if ((progress == (this->tileNum * BUFFER_NUM - 2)) || (progress == (this->tileNum * BUFFER_NUM - 1)))
        {
            AscendC::DataCopy(xLocal, xGm[(progress - LAST_TWO_TILE) * this->tileLength + this->lastTileLength],
                              this->tileLength);
            AscendC::DataCopy(yLocal, yGm[(progress - LAST_TWO_TILE) * this->tileLength + this->lastTileLength],
                              this->tileLength);
        }
        else
        {
            AscendC::DataCopy(xLocal, xGm[progress * this->tileLength], this->tileLength);
            AscendC::DataCopy(yLocal, yGm[progress * this->tileLength], this->tileLength);
        }
        inQueueX.EnQue(xLocal);
        inQueueY.EnQue(yLocal);
    }
    __aicore__ inline void Compute(int32_t progress, AscendC::LocalTensor<float> tmpTensor0)
    {
        AscendC::LocalTensor<int16_t> xLocal = inQueueX.DeQue<int16_t>();
        AscendC::LocalTensor<int16_t> yLocal = inQueueY.DeQue<int16_t>();
        AscendC::LocalTensor<int16_t> zLocal = outQueueZ.AllocTensor<int16_t>();
        AscendC::LocalTensor<float> tmpTensor1 = tmpBuf1.Get<float>();
        AscendC::LocalTensor<float> tmpTensor2 = tmpBuf2.Get<float>();
        AscendC::LocalTensor<float> tmpTensor3 = tmpBuf3.Get<float>();

        AscendC::Cast(tmpTensor1, xLocal, AscendC::RoundMode::CAST_NONE, this->tileLength);
        AscendC::Cast(tmpTensor2, yLocal, AscendC::RoundMode::CAST_NONE, this->tileLength);
        AscendC::Mul(tmpTensor1, tmpTensor1, tmpTensor2, this->tileLength);
        AscendC::Duplicate(tmpTensor3, float(0.0f), this->tileLength);
        AscendC::ReduceSum(tmpTensor3, tmpTensor1, tmpTensor2, this->tileLength);
        AscendC::Add(tmpTensor3, tmpTensor0, tmpTensor3, this->tileLength);
        AscendC::DataCopy(tmpTensor0, tmpTensor3, this->tileLength);
        AscendC::Cast(zLocal, tmpTensor3, AscendC::RoundMode::CAST_RINT, this->tileLength);

        outQueueZ.EnQue<int16_t>(zLocal);
        inQueueX.FreeTensor(xLocal);
        inQueueY.FreeTensor(yLocal);        
    }
    __aicore__ inline void CopyOut(int32_t progress)
    {
        AscendC::LocalTensor<int16_t> zLocal = outQueueZ.DeQue<int16_t>();
        AscendC::DataCopy(zGm[0], zLocal, this->tileLength);
        outQueueZ.FreeTensor(zLocal);
    }

private:
    AscendC::TPipe pipe;
    AscendC::TQue<AscendC::TPosition::VECIN, BUFFER_NUM> inQueueX;
    AscendC::TQue<AscendC::TPosition::VECIN, BUFFER_NUM> inQueueY;
    AscendC::TQue<AscendC::TPosition::VECOUT, BUFFER_NUM> outQueueZ;
    AscendC::TBuf<AscendC::TPosition::VECCALC> tmpBuf0;
    AscendC::TBuf<AscendC::TPosition::VECCALC> tmpBuf1;
    AscendC::TBuf<AscendC::TPosition::VECCALC> tmpBuf2;
    AscendC::TBuf<AscendC::TPosition::VECCALC> tmpBuf3;
    AscendC::GlobalTensor<int16_t> xGm;
    AscendC::GlobalTensor<int16_t> yGm;
    AscendC::GlobalTensor<int16_t> zGm;
    uint32_t blockLength;
    uint32_t tileNum;
    uint32_t tileLength;
    uint32_t lastTileLength;
};


template <>
class KernelDot<int32_t> {
public:
    __aicore__ inline KernelDot() {}
    __aicore__ inline void Init(GM_ADDR x, GM_ADDR y, GM_ADDR z, DotCustomTilingData tiling)
    {   
        if (tiling.isEvenCore)
        {
            this->blockLength = tiling.blockLength;
            this->tileNum = tiling.tileNum;
            this->tileLength = tiling.tileLength / BUFFER_NUM;
            this->lastTileLength = tiling.lastTileLength;

            xGm.SetGlobalBuffer((__gm__ int32_t *)x + this->blockLength * AscendC::GetBlockIdx(), this->blockLength);
            yGm.SetGlobalBuffer((__gm__ int32_t *)y + this->blockLength * AscendC::GetBlockIdx(), this->blockLength);
            zGm.SetGlobalBuffer((__gm__ int32_t *)z + this->blockLength * AscendC::GetBlockIdx(), this->blockLength);
        }
        else
        {
            if (AscendC::GetBlockIdx() < tiling.formerNum)
            {
                this->tileNum = tiling.formerTileNum;
                this->tileLength = tiling.formerTileLength / BUFFER_NUM;
                this->lastTileLength = tiling.formerLastTileLength;

                xGm.SetGlobalBuffer((__gm__ int32_t *)x + tiling.formerLength * AscendC::GetBlockIdx(),
                                    tiling.formerLength);
                yGm.SetGlobalBuffer((__gm__ int32_t *)y + tiling.formerLength * AscendC::GetBlockIdx(),
                                    tiling.formerLength);
                zGm.SetGlobalBuffer((__gm__ int32_t *)z + tiling.formerLength * AscendC::GetBlockIdx(),
                                    tiling.formerLength);
            }
            else
            {
                this->tileNum = tiling.tailTileNum;
                this->tileLength = tiling.tailTileLength / BUFFER_NUM;
                this->lastTileLength = tiling.tailLastTileLength;

                xGm.SetGlobalBuffer((__gm__ int32_t *)x + tiling.formerLength * tiling.formerNum +
                                        tiling.tailLength * (AscendC::GetBlockIdx() - tiling.formerNum),
                                    tiling.tailLength);
                yGm.SetGlobalBuffer((__gm__ int32_t *)y + tiling.formerLength * tiling.formerNum +
                                        tiling.tailLength * (AscendC::GetBlockIdx() - tiling.formerNum),
                                    tiling.tailLength);
                zGm.SetGlobalBuffer((__gm__ int32_t *)z + tiling.formerLength * tiling.formerNum +
                                        tiling.tailLength * (AscendC::GetBlockIdx() - tiling.formerNum),
                                    tiling.tailLength);
            }
        }
        pipe.InitBuffer(inQueueX, BUFFER_NUM, this->tileLength * sizeof(int32_t));
        pipe.InitBuffer(inQueueY, BUFFER_NUM, this->tileLength * sizeof(int32_t));
        pipe.InitBuffer(outQueueZ, BUFFER_NUM, this->tileLength * sizeof(int32_t));
        pipe.InitBuffer(tmpBuf0, this->tileLength * sizeof(float));
        pipe.InitBuffer(tmpBuf1, this->tileLength * sizeof(float));
        pipe.InitBuffer(tmpBuf2, this->tileLength * sizeof(float));
        pipe.InitBuffer(tmpBuf3, this->tileLength * sizeof(float));
    }
    __aicore__ inline void Process()
    {
        int32_t loopCount = this->tileNum * BUFFER_NUM;
        AscendC::LocalTensor<float> tmpTensor0 = tmpBuf0.Get<float>();
        AscendC::Duplicate(tmpTensor0, float(0.0f), this->tileLength);
        for (int32_t i = 0; i < loopCount; i++) {
            CopyIn(i);
            Compute(i, tmpTensor0);
            CopyOut(i);
        }
    }

private:
    __aicore__ inline void CopyIn(int32_t progress)
    {
        AscendC::LocalTensor<int32_t> xLocal = inQueueX.AllocTensor<int32_t>();
        AscendC::LocalTensor<int32_t> yLocal = inQueueY.AllocTensor<int32_t>();
        if ((progress == (this->tileNum * BUFFER_NUM - 2)) || (progress == (this->tileNum * BUFFER_NUM - 1)))
        {
            AscendC::DataCopy(xLocal, xGm[(progress - LAST_TWO_TILE) * this->tileLength + this->lastTileLength],
                              this->tileLength);
            AscendC::DataCopy(yLocal, yGm[(progress - LAST_TWO_TILE) * this->tileLength + this->lastTileLength],
                              this->tileLength);
        }
        else
        {
            AscendC::DataCopy(xLocal, xGm[progress * this->tileLength], this->tileLength);
            AscendC::DataCopy(yLocal, yGm[progress * this->tileLength], this->tileLength);
        }
        inQueueX.EnQue(xLocal);
        inQueueY.EnQue(yLocal);
    }
    __aicore__ inline void Compute(int32_t progress, AscendC::LocalTensor<float> tmpTensor0)
    {
        AscendC::LocalTensor<int32_t> xLocal = inQueueX.DeQue<int32_t>();
        AscendC::LocalTensor<int32_t> yLocal = inQueueY.DeQue<int32_t>();
        AscendC::LocalTensor<int32_t> zLocal = outQueueZ.AllocTensor<int32_t>();
        AscendC::LocalTensor<float> tmpTensor1 = tmpBuf1.Get<float>();
        AscendC::LocalTensor<float> tmpTensor2 = tmpBuf2.Get<float>();
        AscendC::LocalTensor<float> tmpTensor3 = tmpBuf3.Get<float>();

        AscendC::Cast(tmpTensor1, xLocal, AscendC::RoundMode::CAST_NONE, this->tileLength);
        AscendC::Cast(tmpTensor2, yLocal, AscendC::RoundMode::CAST_NONE, this->tileLength);
        AscendC::Mul(tmpTensor1, tmpTensor1, tmpTensor2, this->tileLength);
        AscendC::Duplicate(tmpTensor3, float(0.0f), this->tileLength);
        AscendC::ReduceSum(tmpTensor3, tmpTensor1, tmpTensor2, this->tileLength);
        AscendC::Add(tmpTensor3, tmpTensor0, tmpTensor3, this->tileLength);
        AscendC::DataCopy(tmpTensor0, tmpTensor3, this->tileLength);
        AscendC::Cast(zLocal, tmpTensor3, AscendC::RoundMode::CAST_RINT, this->tileLength);

        outQueueZ.EnQue<int32_t>(zLocal);
        inQueueX.FreeTensor(xLocal);
        inQueueY.FreeTensor(yLocal);      
    }
    __aicore__ inline void CopyOut(int32_t progress)
    {
        AscendC::LocalTensor<int32_t> zLocal = outQueueZ.DeQue<int32_t>();
        AscendC::DataCopy(zGm[0], zLocal, this->tileLength);
        outQueueZ.FreeTensor(zLocal);
    }

private:
    AscendC::TPipe pipe;
    AscendC::TQue<AscendC::TPosition::VECIN, BUFFER_NUM> inQueueX;
    AscendC::TQue<AscendC::TPosition::VECIN, BUFFER_NUM> inQueueY;
    AscendC::TQue<AscendC::TPosition::VECOUT, BUFFER_NUM> outQueueZ;
    AscendC::TBuf<AscendC::TPosition::VECCALC> tmpBuf0;
    AscendC::TBuf<AscendC::TPosition::VECCALC> tmpBuf1;
    AscendC::TBuf<AscendC::TPosition::VECCALC> tmpBuf2;
    AscendC::TBuf<AscendC::TPosition::VECCALC> tmpBuf3;
    AscendC::GlobalTensor<int32_t> xGm;
    AscendC::GlobalTensor<int32_t> yGm;
    AscendC::GlobalTensor<int32_t> zGm;
    uint32_t blockLength;
    uint32_t tileNum;
    uint32_t tileLength;
    uint32_t lastTileLength;
};


template <typename dataType>
class KernelDot{
public:
    __aicore__ inline KernelDot() {}
    __aicore__ inline void Init(GM_ADDR x, GM_ADDR y, GM_ADDR z, DotCustomTilingData tiling)
    {   
        if (tiling.isEvenCore)
        {
            this->blockLength = tiling.blockLength;
            this->tileNum = tiling.tileNum;
            this->tileLength = tiling.tileLength / BUFFER_NUM;
            this->lastTileLength = tiling.lastTileLength;

            xGm.SetGlobalBuffer((__gm__ dataType *)x + this->blockLength * AscendC::GetBlockIdx(), this->blockLength);
            yGm.SetGlobalBuffer((__gm__ dataType *)y + this->blockLength * AscendC::GetBlockIdx(), this->blockLength);
            zGm.SetGlobalBuffer((__gm__ dataType *)z + this->blockLength * AscendC::GetBlockIdx(), this->blockLength);
        }
        else
        {
            if (AscendC::GetBlockIdx() < tiling.formerNum)
            {
                this->tileNum = tiling.formerTileNum;
                this->tileLength = tiling.formerTileLength / BUFFER_NUM;
                this->lastTileLength = tiling.formerLastTileLength;

                xGm.SetGlobalBuffer((__gm__ dataType *)x + tiling.formerLength * AscendC::GetBlockIdx(),
                                    tiling.formerLength);
                yGm.SetGlobalBuffer((__gm__ dataType *)y + tiling.formerLength * AscendC::GetBlockIdx(),
                                    tiling.formerLength);
                zGm.SetGlobalBuffer((__gm__ dataType *)z + tiling.formerLength * AscendC::GetBlockIdx(),
                                    tiling.formerLength);
            }
            else
            {
                this->tileNum = tiling.tailTileNum;
                this->tileLength = tiling.tailTileLength / BUFFER_NUM;
                this->lastTileLength = tiling.tailLastTileLength;

                xGm.SetGlobalBuffer((__gm__ dataType *)x + tiling.formerLength * tiling.formerNum +
                                        tiling.tailLength * (AscendC::GetBlockIdx() - tiling.formerNum),
                                    tiling.tailLength);
                yGm.SetGlobalBuffer((__gm__ dataType *)y + tiling.formerLength * tiling.formerNum +
                                        tiling.tailLength * (AscendC::GetBlockIdx() - tiling.formerNum),
                                    tiling.tailLength);
                zGm.SetGlobalBuffer((__gm__ dataType *)z + tiling.formerLength * tiling.formerNum +
                                        tiling.tailLength * (AscendC::GetBlockIdx() - tiling.formerNum),
                                    tiling.tailLength);
            }
        }
        pipe.InitBuffer(inQueueX, BUFFER_NUM, this->tileLength * sizeof(dataType));
        pipe.InitBuffer(inQueueY, BUFFER_NUM, this->tileLength * sizeof(dataType));
        pipe.InitBuffer(outQueueZ, BUFFER_NUM, this->tileLength * sizeof(dataType));
        pipe.InitBuffer(tmpBuf0, this->tileLength * sizeof(dataType));
    }
    __aicore__ inline void Process()
    {
        int32_t loopCount = this->tileNum * BUFFER_NUM;
        AscendC::LocalTensor<dataType> tmpTensor0 = tmpBuf0.Get<dataType>();
        AscendC::Duplicate(tmpTensor0, dataType(0.0f), this->tileLength);
        for (int32_t i = 0; i < loopCount; i++) {
            CopyIn(i);
            Compute(i, tmpTensor0);
            CopyOut(i);
        }
    }

private:
    __aicore__ inline void CopyIn(int32_t progress)
    {
        AscendC::LocalTensor<dataType> xLocal = inQueueX.AllocTensor<dataType>();
        AscendC::LocalTensor<dataType> yLocal = inQueueY.AllocTensor<dataType>();
        if ((progress == (this->tileNum * BUFFER_NUM - 2)) || (progress == (this->tileNum * BUFFER_NUM - 1)))
        {
            AscendC::DataCopy(xLocal, xGm[(progress - LAST_TWO_TILE) * this->tileLength + this->lastTileLength],
                              this->tileLength);
            AscendC::DataCopy(yLocal, yGm[(progress - LAST_TWO_TILE) * this->tileLength + this->lastTileLength],
                              this->tileLength);
        }
        else
        {
            AscendC::DataCopy(xLocal, xGm[progress * this->tileLength], this->tileLength);
            AscendC::DataCopy(yLocal, yGm[progress * this->tileLength], this->tileLength);
        }
        inQueueX.EnQue(xLocal);
        inQueueY.EnQue(yLocal);
    }
    __aicore__ inline void Compute(int32_t progress, AscendC::LocalTensor<dataType> tmpTensor0)
    {
        AscendC::LocalTensor<dataType> xLocal = inQueueX.DeQue<dataType>();
        AscendC::LocalTensor<dataType> yLocal = inQueueY.DeQue<dataType>();
        AscendC::LocalTensor<dataType> zLocal = outQueueZ.AllocTensor<dataType>();

        AscendC::Mul(xLocal, xLocal, yLocal, this->tileLength);
        AscendC::Duplicate(zLocal, dataType(0.0f), this->tileLength);
        AscendC::ReduceSum(zLocal, xLocal, yLocal, this->tileLength);
        AscendC::Add(zLocal, tmpTensor0, zLocal, this->tileLength);
        AscendC::DataCopy(tmpTensor0, zLocal, this->tileLength);
        AscendC::DumpTensor(zLocal,0,8);

        outQueueZ.EnQue<dataType>(zLocal);
        inQueueX.FreeTensor(xLocal);
        inQueueY.FreeTensor(yLocal);       
    }
    __aicore__ inline void CopyOut(int32_t progress)
    {
        AscendC::LocalTensor<dataType> zLocal = outQueueZ.DeQue<dataType>();
        AscendC::DataCopy(zGm[0], zLocal, this->tileLength);
        outQueueZ.FreeTensor(zLocal);
    }

private:
    AscendC::TPipe pipe;
    AscendC::TQue<AscendC::TPosition::VECIN, BUFFER_NUM> inQueueX;
    AscendC::TQue<AscendC::TPosition::VECIN, BUFFER_NUM> inQueueY;
    AscendC::TQue<AscendC::TPosition::VECOUT, BUFFER_NUM> outQueueZ;
    AscendC::TBuf<AscendC::TPosition::VECCALC> tmpBuf0;
    AscendC::GlobalTensor<dataType> xGm;
    AscendC::GlobalTensor<dataType> yGm;
    AscendC::GlobalTensor<dataType> zGm;
    uint32_t blockLength;
    uint32_t tileNum;
    uint32_t tileLength;
    uint32_t lastTileLength;
};


extern "C" __global__ __aicore__ void dot_custom(GM_ADDR x, GM_ADDR y, GM_ADDR z, DotCustomTilingData tiling)
{
    if(tiling.dataType ==CEIL_BFLOAT16)
    {
        KernelDot<bfloat16_t> op;
        op.Init(x, y, z, tiling);
        op.Process();
    }
    else if(tiling.dataType == CEIL_FLOAT16)
    {
        KernelDot<half> op;
        op.Init(x, y, z, tiling);
        op.Process();
    }
    else if(tiling.dataType == CEIL_FLOAT32)
    {
        KernelDot<float> op;
        op.Init(x, y, z, tiling);
        op.Process();
    }
    else if(tiling.dataType == CEIL_INT8)
    {
        KernelDot<int8_t> op;
        op.Init(x, y, z, tiling);
        op.Process();
    }
    else if(tiling.dataType == CEIL_INT16)
    {
        KernelDot<int16_t> op;
        op.Init(x, y, z, tiling);
        op.Process();
    }
    else if(tiling.dataType == CEIL_INT32)
    {        
        KernelDot<int32_t> op;
        op.Init(x, y, z, tiling);
        op.Process();
    }
    else
    {
        return;
    }
}
